package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

public class TsNewsAnalyse implements AnalyseNews{

	private static final String[] regex={
				"http://.*.ts.cn/.*/html/[0-9]+-[0-9]+/[0-9]+/content_[0-9]+.htm",
				"http://.*.ts.cn/content/[0-9]+-[0-9]+/[0-9]+/content_[0-9]+.htm",
				"http://.*.ts.cn/.*/content/[0-9]+-[0-9]+/[0-9]+/content_[0-9]+.htm",
				"http://topic.ts.cn/[a-z0-9\\/\\-]+/content_[0-9]+.htm"

	};
		
		public boolean isDetailPage(String url) {
			for (int i=0; i < regex.length; i++) {
				if (url.matches(regex[i])) {
					return true;
				}
			}
			return false;
		}

		
		public NewsMeta Update(NewsMeta meta) {
			return null;
		}



		
		public NewsMeta parserHtml(UrlMeta urlMeta) {
			NewsMeta news=new NewsMeta();
			if (urlMeta.getHtml() == null) {
			}
			String htmltxt=urlMeta.getHtml();
			String url=urlMeta.getUrl();

			String title=null;
			String content=null;
			Long date=null;
			Document doc=Jsoup.parse(htmltxt);
			title=doc.select("h1").text();
			if (StringUtils.isBlank(title)){
				title=doc.select("strong[style*=font-size:23px;]").text();
			}

			doc.select("#txt a,#ozoom a").remove();
			content=JSoupUtils.select(doc, "#txt p","#ozoom p");

			if (StringUtils.equalsIgnoreCaseAny(title, "招聘","广告")){
			}

			date=DateUtils.matchDate(doc.select("div.info ").text());
			if (date==null){
				date=DateUtils.matchDate(url);
			}
			String author=null;

			news.setAuthor(author);
			news.setUrl(url);
			news.setTitle(title);
			news.setContent(content);
			news.setDate(date);
			return news;
		}

		public static void main(String[] args) {
			TsNewsAnalyse test=new TsNewsAnalyse();

			String url="http://topic.ts.cn/yiwuhuyangjie/2011-08/01/content_6033005.htm";
			UrlMeta meta=CrawlHTML.responseToURL(url);
			NewsMeta parserHtml=test.parserHtml(meta);
			System.out.println(parserHtml);
		}
	
	public boolean isNeedUpdate(){
		return false;
	}

}
